const domainName = getApp().globalData.domainName
const parameter = getApp().globalData.parameter
const header = getApp().globalData.header

// pages/create_ti/create_ti.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    windowWidth : wx.getSystemInfoSync().windowWidth,
    ti_info:[{ana_pic_list:[]}]
  },

  // 添加图片
  add_pic(){
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFilePath = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          filePath: tempFilePath,
          name: 'files',
          url: domainName + 'edit_ti_upload_pic',
          formData: {ti_id:this.data.ti_info.ti_id},
          header: header,
          success: res => {
            let pic_dic = JSON.parse(res.data).dic
            if(pic_dic.pic_id){
              wx.showToast({
                title: '上传完成！',
              })
              let ti_info = this.data.ti_info
              ti_info.ana_pic_list.push(pic_dic)
              this.setData({ti_info})
            }
          },
        })
      },
    })
  },

  // 修改图片
  update_pic(e){
    var index = e.currentTarget.id
    var pic_id = this.data.ti_info.ana_pic_list[index].pic_id
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFilePath = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          filePath: tempFilePath,
          name: 'files',
          url: domainName + 'edit_ti_update_pic',
          formData: {pic_id},
          header: header,
          success: res => {
            let update_res = JSON.parse(res.data).update_res
            if(update_res){
              wx.showToast({
                title: '修改完成！',
              })

              let ti_info = this.data.ti_info
              ti_info.ana_pic_list[index].pic_url = tempFilePath
              this.setData({ti_info})
            }
          },
        })
      },
    })
  },

  // 删除图片
  delete_pic(e){
    var index = e.currentTarget.id
    var pic_id = this.data.ti_info.ana_pic_list[index].pic_id
    wx.showModal({
      content: '确定要删除该图片吗？',
      title: '警告',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + 'edit_ti_delete_pic',
            data: {pic_id},...parameter,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '删除完成！',
                })
                let ti_info = this.data.ti_info
                ti_info.ana_pic_list.splice(index, 1)
                this.setData({
                  ti_info:ti_info
                })
              }
            },
          })
        }
      },
    })
  },


  // 选择章节
  bindPickerChange(e){
    this.setData({
      'ti_info.chap_id':this.data.chap_list[e.detail.value].id,
      'ti_info.chap_name':this.data.chap_list[e.detail.value].name,
    })
  },


  // 添加答案解析视频
  add_video(){
    wx.chooseMedia({
      count: 1,
      mediaType: ['video'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFilePath = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          filePath: tempFilePath,
          name: 'files',
          url: domainName + 'edit_ti_upload_video',
          formData: {ti_id:this.data.ti_info.ti_id},
          header: header,
          success: res => {
            let video_dic = JSON.parse(res.data).dic
            if(video_dic.video_id){
              wx.showToast({
                title: '上传完成！',
              })
              let ti_info = this.data.ti_info
              ti_info.ana_video_list.push(video_dic)
              this.setData({ti_info})
            }
          },
        })
      },
    })
  },
  // 修改视频
  update_video(e){
    var index = e.currentTarget.id
    var video_id = this.data.ti_info.ana_video_list[index].video_id
    wx.chooseMedia({
      count: 1,
      mediaType: ['video'],
      sizeType: ['compressed'],
      sourceType: ['album'],
      success: res => {
        let tempFilePath = res.tempFiles[0].tempFilePath
        wx.uploadFile({
          filePath: tempFilePath,
          name: 'files',
          url: domainName + 'edit_ti_update_video',
          formData: {video_id},
          header: header,
          success: res => {
            let update_res = JSON.parse(res.data).update_res
            if(update_res){
              wx.showToast({
                title: '修改完成！',
              })

              // let ti_info = this.data.ti_info
              // ti_info.ana_video_list[index].video_url = tempFilePath
              // this.setData({ti_info})
              this.setData({
                ['ti_info.ana_video_list[' + index + '].video_url'] : tempFilePath
              })
            }
          },
        })
      },
    })
  },

  // 删除视频
  delete_video(e){
    var index = e.currentTarget.id
    var video_id = this.data.ti_info.ana_video_list[index].video_id
    wx.showModal({
      content: '确定要删除该视频吗？',
      title: '警告',
      success: res => {
        if(res.confirm){
          wx.request({
            url: domainName + 'edit_ti_delete_video',
            data: {video_id},...parameter,
            success: res => {
              if(res.data.delete_res){
                wx.showToast({
                  title: '删除完成！',
                })

                let ti_info = this.data.ti_info
                ti_info.ana_video_list.splice(index, 1)
                this.setData({
                  ti_info:ti_info
                })
              }
            },
          })
        }
      },
    })
  },


  // 表单数据
  submit(e) {
    if(this.data.ti_info.chap_id){
      wx.showModal({
        cancelColor: '#1aad19',
        cancelText: '取消',
        confirmColor: '#999',
        confirmText: '提交',
        content: '确定提交吗？',
        showCancel: true,
        title: '提示',
        success: res=> {
          if(res.confirm){
            this.edit_ti()
          }
        },
      })
    }else{
      wx.showModal({
        content: '请选择该题所属章节',
        showCancel: false,
        title: '提示',
      })
    }
  },

  // 上传数据
  edit_ti(){
    // console.log('上传数据')
    let ti_info = this.data.ti_info
    console.log('ti_info',ti_info)
    wx.request({
      url: domainName + '/edit_ti_submit',
      data: {ti_info:JSON.stringify(ti_info)},...parameter,
      success: res => {
        // console.log('上传数据',res.data)

        if(res.data.update_res){
          wx.showModal({
            showCancel: false,
            title: '提交完成',
            success: res => {
              wx.navigateBack()
            },
          })
        }
      },
    })
    
  },

  // 取消添加
  back_btn(){
    wx.navigateBack({
      delta: 1,
    })
  },

  // 继续添加
  continue_add(){
    this.setData({
      ti_list:[]
    })
  },  

  // 检查该习题是否重复
  question(e){
    var question = e.detail.value
    this.setData({
      'ti_info.question':question,
    })
  },

  a(e){
    this.setData({
      'ti_info.a':e.detail.value,
    })
  },

  b(e){
    this.setData({
      'ti_info.b':e.detail.value,
    })
  },

  c(e){
    this.setData({
      'ti_info.c':e.detail.value,
    })
  },

  answer(e){
    this.setData({
      'ti_info.answer':e.detail.value,
    })
  },

  analysis(e){
    this.setData({
      'ti_info.analysis':e.detail.value,
    })
  },

  // 查询类似的习题列表
  search_ti_list(question){
    wx.request({
      url: domainName + '/search_ti_list',
      data: {
        input_q:question
      },...parameter,
      success: res => {
        this.setData({
          ti_list:res.data.ti_list || [],
        })
      },
    })
  },


  // 加载章节列表
  page_init(){
    wx.request({
      url: domainName + '/edit_ti_init',
      data: {},...parameter,
      success: res => {
        this.setData({
          chap_list:res.data.chap_list,
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      ti_info:JSON.parse(decodeURIComponent(options.ti_info))
    })
    this.page_init()
  },

})